package servlet;

import entity.User;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import org.apache.commons.codec.digest.DigestUtils;
import service.UserDaoService;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        HttpSession session=request.getSession();
        //获取当前时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp now = new Timestamp(System.currentTimeMillis());


        String email = request.getParameter("email");
        String name = request.getParameter("name");
        String pasd = request.getParameter("pasd");
        String MD5pasd = DigestUtils.md5Hex(pasd);
        User user = new User();
        user.setUsername(name);
        user.setEmail(email);
        user.setUpassword(MD5pasd);
        user.setSex(3);//性别 0男 1女 3保密
        user.setIsDelete(1);// 删除状态  1未删除  0删除
        user.setCreateTime(now);
        UserDaoService register = new UserDaoService();
        int count = register.Register(user);
        if(count == 1){
            out.print("<script language='javascript'>alert('注册成功!'); window.location.href='./WinterTourism/material/login.jsp'; </script>");
        }else{
            out.print("<script language='javascript'>alert('注册失败!'); window.location.href='./WinterTourism/material/register.jsp'; </script>");
        }
    }
}
